import Taro from '@tarojs/taro';

import {Command} from '../constant';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';

import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },

    async findEscorttradeList() {
      //分页查询发布的配送订单
      let selectDistance = Taro.getStorageSync('selectDistance');
      let {pageNum, pageSize, orderList} = getData().main;
      const params = {
        // communityId: selectDistance?.id,//社区id
        // courierStationId: 0,//驿站id
        // courierStationName: '',//驿站名称
        // escortTradePay: 0,//支付方式，0：微信支付，1：余额支付，2：积分支付，3：积分+微信，4：积分+余额
        // escortTradePayStatus: '',//配送订单的支付状态，0：未付款，1：已付款，2：已退款
        // escortTradeStatus,//订单状态，0：待支付，1：待接单，2：服务中，3：已完成，4：已取消
        pageNum, //第几页
        pageSize, //每页显示多少条
      };
      const result = await api.escortTradeController.findEscorttradeList(params);
      console.log('---------------result');
      console.log(result);
      if (result.code == 'K-000000') {
        this.commonChange('main.totalPages', result.context.totalPages);
        if (pageNum == 0) {
          action.commonChange('main.orderList', result.context.content);
        } else {
          let newList = orderList.concat(result.context.content);
          action.commonChange('main.orderList', newList);
        }
      } else {
        Taro.showToast({
          title: result.context,
          icon: 'none',
          duration: 2000,
        });
      }
    },

    async escortsnatchorderAdd(escortTradeId) {
      //接单
      const params = {
        escortTradeId, //订单id
      };
      const result = await api.escortTradeController.escortsnatchorderAdd(params);
      if (result.code == 'K-000000') {
        Taro.showToast({
          title: '接单成功！',
          icon: 'none',
          duration: 2000,
        });
        setTimeout(() => {
          Taro.navigateTo({
            url: '/pages/package-D/lifeOrders/index?orderType=2',
          });
        }, 1000);
      } else {
        Taro.showToast({
          title: result.message,
          icon: 'none',
          duration: 2000,
        });
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packagePickupExpressDeliveryDMain'),
  };
}
